package org.jeecg.modules.foc;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.ObjectPool;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.Md5Util;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.common.FocRequest;
import org.jeecg.modules.common.FocRespone;
import org.jeecg.modules.foc.entity.*;
import org.jeecg.modules.foc.mapper.FocPointDataMapper;
import org.jeecg.modules.foc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "Foc数据推送")
@RestController
@RequestMapping("/north")
public class FocPushController {

    @Value("${ems.url}")
    public String emsUrl;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private FocPointDataMapper focPointDataMapper;
    @Autowired
    private IFocAlarmService focAlarmService;
    Snowflake snowflake = IdUtil.getSnowflake(1, 1);


    /**
     * 实时数据接收
     */
    //@AutoLog(value = "foc操作-实时数据接收")
    @ApiOperation(value = "foc操作-实时数据接收", notes = "foc操作-实时数据接收")
    @RequestMapping(value = "/online_data_strategy_push")
    public FocRespone onlineDataStrategyPush(@RequestBody String body) {
        //获取请求体中的数据
        FocPushPorintDataDTO pushPorintDataDTO = JSONObject.parseObject(JSONObject.parseObject(body).getString("data"), FocPushPorintDataDTO.class);
        //解析推送过来的数据
        List<DevicePishDTO> devices = pushPorintDataDTO.getDevices();
        for (DevicePishDTO devicePishDTO : devices) {
            List<PontPustDTO> points = devicePishDTO.getPoints();
            for (PontPustDTO pontPustDTO : points) {
                //保存点位数据
                FocPointData focPointData = new FocPointData();
                focPointData.setStrategyId(pushPorintDataDTO.getStrategy_guid());
                focPointData.setPeriod(pushPorintDataDTO.getPeriod());
                focPointData.setGuid(devicePishDTO.getGuid());
                focPointData.setStatus(devicePishDTO.getStatus());
                focPointData.setPortGuid(pontPustDTO.getGuid());
                focPointData.setValue(pontPustDTO.getValue());
                focPointData.setTimestam(pontPustDTO.getTimestamp());
                focPointData.setDelFlag(0);
                focPointData.setCreateBy("ems");
                focPointData.setUpdateBy("ems");
                focPointData.setCreateTime(new Date());
                //生成一个自增的雪花算法id
                focPointData.setId(snowflake.nextId());
                focPointDataMapper.insert(focPointData);
            }
        }

        return FocRespone.ok();
    }





}
